import axios from "axios";

// 创建 axios 实例
const request = axios.create({
  // API 请求的默认前缀
  // baseURL: 'http://192.168.0.192:8080',
  baseURL: "https://supervision.yichangct.com/api",
  // baseURL: "http://127.0.0.1:8080",
  // baseURL: 'https://aidex.setworld.net',
  timeout: 20000, // 请求超时时间
});

// request interceptor
request.interceptors.request.use((config) => {
  // const token = storage.get(ACCESS_TOKEN)
  // // 如果 token 存在
  // // 让每个请求携带自定义 token 请根据实际情况自行修改
  // if (token) {
  //   config.headers["Authorization"] = "Bearer " + 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjQ3ZGY3ZWJjLTA3NDEtNDAyMC1iNWVkLTAxMGVlNTk4MjNlMiJ9.eAo8uqB1heCLhhEBMur4FhQVrxZ8OkW7DJ5RaKGRUBrxAamihrKO3FUOeLd9Q9_PqLNPKt5yY4juXbUrtFmnhQ' // 让每个请求携带自定义token 请根据实际情况自行修改
  //   // config.headers['accessAccess-Token'] = token
  // }
  // config.headers["Authorization"] = "Bearer " + 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjQ3ZGY3ZWJjLTA3NDEtNDAyMC1iNWVkLTAxMGVlNTk4MjNlMiJ9.eAo8uqB1heCLhhEBMur4FhQVrxZ8OkW7DJ5RaKGRUBrxAamihrKO3FUOeLd9Q9_PqLNPKt5yY4juXbUrtFmnhQ' // 让每个请求携带自定义token 请根据实际情况自行修改
  // get请求映射params参数
  if (config.method === "get" && config.params) {
    let url = config.url + "?";
    for (const propName of Object.keys(config.params)) {
      const value = config.params[propName];
      var part = encodeURIComponent(propName) + "=";
      // 修改漏洞
      if (value != null && typeof value !== "undefined") {
        if (typeof value === "object") {
          for (const key of Object.keys(value)) {
            const params = propName + "[" + key + "]";
            var subPart = encodeURIComponent(params) + "=";
            url += subPart + encodeURIComponent(value[key]) + "&";
          }
        } else {
          url += part + encodeURIComponent(value) + "&";
        }
      }
    }
    url = url.slice(0, -1);
    config.params = {};
    config.url = url;
  }
  return config;
});

// response interceptor
request.interceptors.response.use((res) => {
  // 请求rul
  // const requestUrl = res.config.url;
  // 未设置状态码则默认成功状态
  const code = res.data.code || 200;
  // 获取错误信息
  const msg = res.data.msg;
  if (code !== 200) {
    this.notification.error({
      message: msg,
      duration: 5,
    });
  } else {
    return res.data;
  }
  return Promise.reject(msg);
});

export default request;
